using System;
using System.Collections;
using System.Collections.Generic;
using cfg.Myskill;
using QFramework;
using UnityEngine;

public class SkillManager :  MonoSingleton<SkillManager>
{
    private Dictionary<string, SkillBase> skillDictionary;
    
    private void Awake()
    {
        skillDictionary = new Dictionary<string, SkillBase>();
        
        //初始化技能
        InitSkills();
    }

    private void InitSkills()
    {
        ClearSkills();
        //冲刺技能同时只能存在一个
        skillDictionary.Add(Setting.curDashSkill,new DashSkill(Setting.curDashSkill));
        //剑技能同时只能存在一个
        skillDictionary.Add(Setting.curSwordSkill,new ThrowSwordSkill(Setting.curSwordSkill));
        //黑洞技能
        skillDictionary.Add(Setting.curHoleSkill,new BlackHoleSkill(Setting.curHoleSkill));
        //水晶技能
        skillDictionary.Add(Setting.curCrystalSkill,new CrystalSkill(Setting.curCrystalSkill));
        //反击技能
        skillDictionary.Add(Setting.curCounterSkill,new CounterSkill(Setting.curCounterSkill));
    }
    
    private void Update()
    {
        foreach (var keyValuePair in skillDictionary)
        {
            keyValuePair.Value.Update();
        }
    }

    public T GetSkill<T>(string skillName) where T:SkillBase
    {
        if (skillDictionary.TryGetValue(skillName,out SkillBase skillBase))
        {
            return skillBase as T;
        }

        return null;
    }

    /// <summary>
    /// 创造克隆体
    /// </summary>
    /// <param name="prefab">预制体</param>
    /// <param name="pos">位置</param>
    /// <param name="faceDir">朝向</param>
    /// <param name="skillName">复制技能</param>
    /// <typeparam name="T">复制技能脚本</typeparam>
    public void CreateClone<T>(string prefab,Vector2 pos,int faceDir,string skillName)where T:CloneBaseSkill
    { 
        var skill = GetSkill<T>(skillName); 
        skill.Clone(prefab,pos,faceDir);
    }

    /// <summary>
    /// 创造残影攻击
    /// </summary>
    /// <param name="pos"></param>
    /// <param name="faceDir"></param>
    /// <param name="skillName"></param>
    /// <param name="hitBack"></param>
    public void CreateCloneAttack( Vector2 pos, int faceDir, string skillName,bool hitBack)
    {
        GameObject cloneObj = PoolMgr.Instance.GetObj("ClonePlayer");
        var  cloneController = cloneObj.GetComponent<PlayerCloneController>();
        var skillInfo = LoadCofigManager.Instance.GetSkillInfo(skillName).Info;
        cloneController.Init(skillInfo as CloneBase, pos,faceDir);
        cloneController.PlayAttack();
        cloneController.SetCanHitBack(hitBack);
        
    }
    
    //使用协程 等待0.2秒创造残影攻击
    public void CreateCloneAttackWithCoroutine( Vector2 pos, int faceDir, string skillName,bool hitBack,float sec =0.2f)
    {
        StartCoroutine(CreateCloneAttackWithCoroutineCoroutine(pos,faceDir,skillName,hitBack,sec));
    }

    private IEnumerator CreateCloneAttackWithCoroutineCoroutine(Vector2 pos, int faceDir, string skillName, bool hitBack,float sec =0.2f)
    {
        yield return  new WaitForSeconds(sec);
        CreateCloneAttack(pos,faceDir,skillName,hitBack);
    }

    /// <summary>
    /// 是否有技能发动中
    /// </summary>
    /// <returns></returns>
    public bool IsSkillInUse()
    {
        foreach (var keyValuePair in skillDictionary)
        {
            if (keyValuePair.Value.skillDurationing)
            {
                return  true;   
            }
        }
        
        return false;
    }
    
    /// <summary>
    /// 某技能发动中
    /// </summary>
    /// <param name="skillId"></param>
    /// <returns></returns>
    public bool IsSkillInUse(string skillId)
    {
        foreach (var keyValuePair in skillDictionary)
        {
            if (keyValuePair.Key == skillId)
            {
                return keyValuePair.Value.skillDurationing;
            }
        }
        
        return false;
    }

    public void OnDisable()
    {
        ClearSkills();
    }

    private void ClearSkills()
    {
        
        foreach (var keyValuePair in skillDictionary)
        {
            keyValuePair.Value.OnDestroy();
        }
        skillDictionary.Clear();
    }
}
